package com.neu.dao.wty;

import com.neu.dao.BaseDao;
import com.neu.util.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class UserDao extends BaseDao {
    static QueryRunner qr=new QueryRunner(DBUtils.getDataSource());

    public List<Map<String,Object>> checkLogin(String u_tel,String u_pwd){
        List<Map<String,Object>> list=null;
        String sql="select * from t_user where 1=1" +
                /*select u.* , a.a_id from t_address a ,t_user u where 1=1*/
                   " and u_tel=?" +
                   " and u_pwd=?";
//                +" and a.u_id=u.u_id";

        try {
            list=qr.query(sql,new MapListHandler(),u_tel,u_pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public int register(String u_tel,String u_pwd){
        int row=0;
        String sql=" insert into t_user(u_id,u_tel,u_pwd,u_date)" +
                "    values (seq_id.nextval,?,?,sysdate)";

        try {
            row=qr.update(sql,u_tel,u_pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    public int alterPwd(String newPwd,String u_mail){
        int row=0;
        String sql="update t_user set u_pwd=? where u_email=?";
        try {
            row=qr.update(sql,newPwd,u_mail);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    public int findByEmail(String u_email){
        int row=0;
        String sql="select * from t_user where u_email=?";
        try {
            row=qr.update(sql,u_email);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

}
